<?php

class DoitacKhachhangController extends Controller
{

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array(''),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('index','kiemtrasophieu','taithanhcong','khachhangexcel','doitacexcel','khachhang','view','create','doitac','update','admin','delete','nhaphoadon','nhapmothoadon','getid'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array(''),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionKiemTraSoPhieu()
	{
		if (isset($_POST['phieu'])) {
			$phieu = $_POST['phieu'];
			$model = DoitacKhachhang::model()->findAll("so_phieu_thanh_toan ='{$phieu}'");
			if ($model == null) {
				echo "0";
			}else{
				echo "1";
			}
		}
	}

	public function actionGetId()
	{
		header('Access-Control-Allow-Origin: *');
		header('Content-type: application/json');
		if (isset($_POST['idhoadon'])) {
			$id = $_POST['idhoadon'];
			$hoadon = DoitacKhachhang::model()->findByPk($id);
			if ($hoadon != null) {
				$arrs = array(
					'tendoitac'=>$hoadon->doitac->ten,
					'iddoitac'=>$hoadon->doitac->id,
					'tenkhachhang'=>$hoadon->khachhang->ten,
					'idkhachhang'=>$hoadon->khachhang->id,
					'phieu'=>$hoadon->so_phieu_thanh_toan,
					'tien'=>$hoadon->so_tien,
				);
				echo json_encode($arrs);
			}
		}
	}
	public function actionNhapHoaDon()
	{
		if (isset($_GET['id'])) {
			$id =  $_GET['id'];
			$data = DoitacKhachhang::model()->search("id_doitac = $id");
		}else{
			$data = DoitacKhachhang::model()->search();
		}
		$this->render('nhaphoadon',array('data'=>$data));
	}
	public function actionNhapMotHoaDon($id)
	{
		$model = Doitac::model()->findByPk($id);
		if ($model == null) {
			throw new CHttpException(404,'The requested page does not exist.');
		}
		$data = DoitacKhachhang::model()->search("id_doitac = $id");
		$this->render('nhapmothoadon',array('data'=>$data,'model'=>$model));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		header('Access-Control-Allow-Origin: *');
		header('Content-type: application/json');
		if (isset($_POST['idKhachhang']) && isset($_POST['idDoitac']) && isset($_POST['phieu']) && isset($_POST['tien']) && isset($_POST['id']) && isset($_POST['date'])) {
			$idDoitac = $_POST['idDoitac'];
			$idKhachhang = $_POST['idKhachhang'];
			$phieu = $_POST['phieu'];
			$tien = $_POST['tien'];
			$id = $_POST['id'];
			$doitac = Doitac::model()->findByPk($idDoitac);
			if ($doitac != null) {
				$kh = Khachhang::model()->findByPk($idKhachhang);
				if ($kh != null) {
					$dkh = DoitacKhachhang::model()->findByPk($id);
					if ($dkh == null) {
						$dkh = new DoitacKhachhang;
					}
					$dkh->id_khachhang = $kh->id;
					$dkh->id_doitac = $doitac->id;
					$dkh->ms_khachhang = $kh->ma_so;
					$dkh->so_phieu_thanh_toan = $phieu;
					$so_tien_cu = $dkh->so_tien;
					$dkh->so_tien = $tien;
					$dkh->chiet_khau = ceil($tien * $doitac->cam_ket / 100);
					$dkh->chua_thanh_toan_kh = $dkh->chiet_khau;
					$dkh->chua_thanh_toan_dt = ceil($tien *( $doitac->cam_ket  + $doitac->chi_phi_dv)/100);
					$dkh->ngay_tao = date("Y-m-d");
					$dkh->ngay_su_dung = date("Y-m-d",strtotime($_POST['date']));
					$kh->chua_thanh_toan += $dkh->chua_thanh_toan_kh;
					$kh->tong_tieu_dung += $dkh->so_tien;
					$kh->duoc_chiet_khau += $dkh->chiet_khau;
					$doitac->tong_chi_phi += $tien * $doitac->chi_phi_dv / 100;
					$doitac->tong_doanh_thu += $dkh->so_tien;
					$doitac->tong_chiet_khau += $dkh->chiet_khau;
					$doitac->chua_thanh_toan += $dkh->chua_thanh_toan_dt;
					$doitac->save(false);
					$kh->save(false);
					$dkh->save(false);
					echo json_encode(array('status'=>true,'info'=>"Đã thêm hóa đơn"));
				}
			}else{
				echo json_encode(array('status'=>false,'info'=>"Thất bại"));
			}
		}
	}
	public function actionTaiThanhCong()
	{
		$this->render('taithanhcong');
	}
	public function actionDoiTacExcel($id)
	{
		$model = Doitac::model()->findByPk($id);
		if ($model == null) {
			throw new CHttpException(404,'The requested page does not exist.');
		}
		if (isset($_POST['Doitac'])) {
			$khachhang = isset($_POST['Doitac']['khachhang']) ? $_POST['Doitac']['khachhang'] : "";
			$trangthai = isset($_POST['Doitac']['trangthai_thanhtoan']) ? $_POST['Doitac']['trangthai_thanhtoan'] : "";
			$so_luong = isset($_POST['Doitac']['so_luong']) ? $_POST['Doitac']['so_luong'] : 0;
			
			$criteria=new CDbCriteria; 
			$condition = "id_doitac ={$id}";
			if ($trangthai != "") {
				$condition .= " and trangthai_thanhtoan_dt = {$trangthai}";
			}
			if ($khachhang != "") {
				$condition .= " and id_khachhang = {$khachhang}";
			}
			if (!is_int($so_luong)) {
				$so_luong = 0;
			}
			$criteria->condition = $condition;
			if ($so_luong > 0) {
				$criteria->limit = $so_luong;
			}
			$list = DoitacKhachhang::model()->findAll($criteria);
			DoitacKhachhang::createExcelDoiTac($model,$list);
		}
		$this->render('doitacexcel',array('model'=>$model));
	}
	public function actionDoiTac($id)
	{
		$model = Doitac::model()->findByPk($id);
		if ($model == null) {
			throw new CHttpException(404,'The requested page does not exist.');
		}
		$data = new DoitacKhachhang('search');
		$data->unsetAttributes();  // clear any default values
		if(isset($_GET['DoitacKhachhang']))
			$data->attributes=$_GET['DoitacKhachhang'];
		$this->render('doitac',array('data'=>$data,'model'=>$model));
	}
	public function actionKhachHang($id)
	{
		$model = Khachhang::model()->findByPk($id);
		if ($model == null) {
			throw new CHttpException(404,'The requested page does not exist.');
		}
		$data = new DoitacKhachhang('search');
		$data->unsetAttributes();  // clear any default values
		if(isset($_GET['DoitacKhachhang']))
			$data->attributes=$_GET['DoitacKhachhang'];
		$this->render('khachhang',array('data'=>$data,'model'=>$model));
	}
	public function actionKhachHangExcel($id)
	{
		$model = Khachhang::model()->findByPk($id);
		if ($model == null) {
			throw new CHttpException(404,'The requested page does not exist.');
		}
		if (isset($_POST['Khachhang'])) {
			$khachhang = isset($_POST['Khachhang']['doitac']) ? $_POST['Khachhang']['doitac'] : "";
			$trangthai = isset($_POST['Khachhang']['trangthai_thanhtoan']) ? $_POST['Khachhang']['trangthai_thanhtoan'] : "";
			$so_luong = isset($_POST['Khachhang']['so_luong']) ? $_POST['Khachhang']['so_luong'] : 0;
			// var_dump($khachhang,$trangthai,$so_luong);
			$criteria=new CDbCriteria; 
			$condition = "id_khachhang ={$id}";
			if ($trangthai != "") {
				$condition .= " and trangthai_thanhtoan_kh = {$trangthai}";
			}
			if ($khachhang != "") {
				$condition .= " and id_doitac = {$khachhang}";
			}
			$criteria->condition = $condition;
			if (!is_int($so_luong)) {
				$so_luong = 0;
			}
			if ($so_luong > 0) {
				$criteria->limit = $so_luong;
			}
			$list = DoitacKhachhang::model()->findAll($criteria);
			DoitacKhachhang::createExcelKhachHang($model,$list);
		}
		$this->render('khachhangexcel',array('model'=>$model));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return DoitacKhachhang the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=DoitacKhachhang::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param DoitacKhachhang $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='doitac-khachhang-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
